package com.ruoyi.know.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.know.domain.UserComments;
import com.ruoyi.know.service.IPointRecordsService;
import com.ruoyi.know.service.IUserCommentsService;
import com.ruoyi.know.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户评论Controller
 *
 * @author ruoyi
 * @date 2025-02-03
 */
@RestController
@RequestMapping("/know/comments")
public class UserCommentsController extends BaseController
{
    @Autowired
    private IUserCommentsService userCommentsService;
    @Autowired
    private IPointRecordsService pointRecordsService;
    @Autowired
    private SensitiveWordService sensitiveWordService;

    @GetMapping("/tree")
    public AjaxResult getCommentTree(
            @RequestParam Long infoId,
            @RequestParam Long commentType) {

        List<UserComments> commentTree = userCommentsService.getCommentTree(infoId, commentType);
        return success(commentTree);
    }
    /**
     * 查询用户评论列表
     */
    @PreAuthorize("@ss.hasPermi('know:comments:list')")
    @GetMapping("/list")
    public AjaxResult list(UserComments userComments)
    {
        List<UserComments> list = userCommentsService.selectUserCommentsList(userComments);
        return success(list);
    }

    /**
     * 导出用户评论列表
     */
    @PreAuthorize("@ss.hasPermi('know:comments:export')")
    @Log(title = "用户评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserComments userComments)
    {
        List<UserComments> list = userCommentsService.selectUserCommentsList(userComments);
        ExcelUtil<UserComments> util = new ExcelUtil<UserComments>(UserComments.class);
        util.exportExcel(response, list, "用户评论数据");
    }

    /**
     * 获取用户评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:comments:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(userCommentsService.selectUserCommentsByCommentId(commentId));
    }

    /**
     * 新增用户评论
     */
    @PreAuthorize("@ss.hasPermi('know:comments:add')")
    @Log(title = "用户评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserComments userComments)
    {
        //替换敏感词
        String content = userComments.getContent();
        //20250402不替换敏感词
        userComments.setContent(content);
//        userComments.setContent(sensitiveWordService.replaceWithImgTag(content));
        userComments.setUserId(getLoginUser().getUserId());
        userComments.setCreateAt(new Date());
        //加积分
        pointRecordsService.earnPoints(getUserId(),"评论",true);
        return toAjax(userCommentsService.insertUserComments(userComments));
    }

    /**
     * 修改用户评论
     */
    @PreAuthorize("@ss.hasPermi('know:comments:edit')")
    @Log(title = "用户评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserComments userComments)
    {
        return toAjax(userCommentsService.updateUserComments(userComments));
    }

    /**
     * 删除用户评论
     */
    @PreAuthorize("@ss.hasPermi('know:comments:remove')")
    @Log(title = "用户评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentId}")
    public AjaxResult remove(@PathVariable Long commentId)
    {
        //减积分
        pointRecordsService.earnPoints(getUserId(),"评论",false);
        boolean res = userCommentsService.deleteUserCommentsByCommentId(commentId);
        if (res){
            return toAjax(userCommentsService.deleteUserCommentsByCommentId(commentId));
        }else{
            return AjaxResult.error("该评论存在子评论，不能删除");
        }
    }
}
